﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ADShop.Models;
namespace ADShop.Controllers
{
    public class ShoppingCartController : Controller
    {
        //
        // GET: /ShoppingCart/
        #region "Variable"
        //ADShopEntities adshop = new ADShopEntities();
        EShopEntities1 adshop = new EShopEntities1();
        #endregion
        public ActionResult Index()
        {
            ShoppingCartModels model = new ShoppingCartModels();
            model.Cart = (ShoppingCart)Session["Cart"];
            return View(model);
        }
        [HttpPost]

        public ActionResult UseVoucher(FormCollection fc)
        {
            string Voucher = fc["voucher"];
            if (Voucher != null)
            {
                (Session["Cart"] as ShoppingCart).PercentVoucher = (int)adshop.VoucherProduct.Where(d => d.VoucherCode.Equals(Voucher)).SingleOrDefault().Discounts;
            }
            return RedirectToAction("Index");
        }

        public JsonResult AddToCart(int id)
        {
            var response = new { Code = 1, Msg = "Fail" };
            Product newProducts = GetProductViaID(id);
            if(newProducts != null)
            {
                ShoppingCart objCart = (ShoppingCart)Session["Cart"];
                if(objCart == null)
                {
                    objCart = new ShoppingCart();
                }

                ShoppingCartItems newItems = new ShoppingCartItems()
                {
                    ProductID = newProducts.IDProduct,
                    ProductName = newProducts.NameProduct,
                    Reward =  Convert.ToInt32(newProducts.Rewards),
                    ProductImages = newProducts.ProductImage,
                    Price = newProducts.ListedPrice,
                    Quantity = 1,
                    TotalPrice = newProducts.ListedPrice
                };
                objCart.AddToCart(newItems);
                Session["Cart"] = objCart;
                response = new { Code = 0, Msg = "Success" };
            }
            return Json(response);
        }

        public JsonResult RemoveFromCart(int id)
        {
            var response = new { Code = 1, Msg = "Fail" };

            ShoppingCart objCart = (ShoppingCart)Session["Cart"];
            if (objCart != null)
            {
                objCart.RemoveFromCart(id);
                Session["Cart"] = objCart;
                response = new { Code = 0, Msg = "Success" };
            }
            return Json(response);
        }

        [HttpPost]
        public JsonResult UpdateQuantity(int id,int quantity)
        {
            var response = new { Code = 1, Msg = "Fail" };

            ShoppingCart objCart = (ShoppingCart)Session["Cart"];
            if (objCart != null)
            {
                objCart.UpdateQuantity(id, quantity);
                Session["Cart"] = objCart;
                response = new { Code = 0, Msg = "Success" };
            }
            return Json(response);
        }



        public Product GetProductViaID(int id)
        {
            Product p = new Product();
            p.IDProduct = id;
            p.NameProduct = adshop.Product.Where(d => d.IDProduct == id).FirstOrDefault().NameProduct;
            p.ListedPrice = adshop.Product.Where(d => d.IDProduct == id).FirstOrDefault().ListedPrice;
            if(adshop.ImagesProduct.Any(d => d.IDProduct == id))
            {
                p.ProductImage = adshop.ImagesProduct.Where(d => d.IDProduct == id).FirstOrDefault().LinkImagesProduct; 
            }
            else
            {
                p.ProductImage = "http://www.wwubap.org/wp-content/uploads/2012/03/no-available-image.png";
            }
            return p;
        }
    }
}
